<%- include('css') %>
<div id="app" style="width:980px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>新建后台管理员</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">真实姓名</label>
            <div class="layui-input-block">
                <input type="text" name="realname" lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">登录账户</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required|username" placeholder="请输入账号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系手机</label>
            <div class="layui-input-block">
                <input type="text" maxlength="11" name="phone" lay-verify="required|phone" placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">角色</label>
            <div class="layui-input-block">
                <select name="role_id" lay-verify="required" lay-search="" lay-filter="role" id="role_select" >
                    <option value="" >直接选择或搜索选择</option> <!-- v-if="item.id == "-->
                    <option :value="item.id" v-for="(item,index) in roles" :key="index">{{item.name}}</option>
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea type="text" name="remark" autocomplete="off" placeholder="请输入备注" class="layui-textarea"></textarea>
            </div>
        </div>


        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="save">新建</button>
                <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>

<%- include('script') %>
<script>

    var vm = new Vue({
        el: '#app',
        data:function(){

            return {
                isButtonDisabled:false,
                load:false,
                roles:[]//角色数组
            }

        },
        mounted:function(){

            var self = this;

            layui.use(['form'], function () {
                var form = layui.form;

                var load = layer.load();

                var v = setInterval(()=>{
                    if(self.load){
                        form.render('select');
                        clearInterval(v);
                        layer.close(load);
                    }
                },1000)

                self.getroles(load,v);

                //定义验证规则
                form.verify({
                    username:[/^[a-zA-Z][a-zA-Z0-9_]{4,15}$/,'字母开头5-16位字母数字_']
                });

                //监听提交
                form.on('submit(save)', function (data) {
                    self.isButtonDisabled = true;
                    $.ajax({
                        url:'/admin/admin',
                        type:'POST',
                        data:{data:data.field},
                        success:(res)=>{
                            if(res.code === 200){
                                parent.vm.iframesave()
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.layer.msg('新建成功!',{icon:1})
                            }else{
                                layer.msg('新建失败:'+res.error+' -- '+res.code,{icon:2})
                            }
                            self.isButtonDisabled = false;
                        }
                    });
                    return false;
                });

            });
        },

        methods:{

            doclose:function(){
                parent.vm.iframeclose();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },
            getroles:function(load,v){
                let self = this;
                $.ajax({
                url:'/admin/role',
                type:'GET',
                data:{isPage:false},
                success:(res)=>{
                    if(res.code == 0){
                        self.roles = res.data;
                        self.load = true;
                    }else{
                        clearInterval(v);
                        layer.close(load);
                        layer.msg('获取角色数据失败：'+res.error+' -- '+res.code,{icon:2})
                    }
                },
                fail:(res)=>{
                    clearInterval(v);
                    layer.close(load);
                    layer.msg('获取角色数据失败!  原因：请求发起失败',{icon:2})
                }
                });
            }
        }
    })
</script>
